package com.freework.tree;


import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;


public class TreeReadDataService implements TreeReadDataServiceI{

	public TreeDatas getMapTreeData(Map map, String idp, String pidp,
			String textp,String codep) {
		TreeDatas td = new TreeDatas();
		Object id = map.get(idp);
		Object pid = map.get(pidp);
		Object text = map.get(textp);
		Object code = map.get(codep);
		td.setId(id != null ? Integer.parseInt(id.toString()) : 0);
		td.setPid(pid != null ?Integer.parseInt(idp.toString()): 0);
	
		td.setText(text != null ?(code!=null?code+"-"+text:text.toString()) : "");
		td.setCode(code != null ? code.toString() : "");

		return td;
	}

	public TreeDatas getTreeData(Method[]methods,Object obj) throws IllegalAccessException,
			InvocationTargetException, NoSuchMethodException {
		TreeDatas td = new TreeDatas();

		Object id = methods[0].invoke(obj);
		Object pid = methods[1].invoke(obj);
		Object text = methods[2].invoke(obj);
		Object code =methods[3].invoke(obj);

		td.setId(id instanceof Integer? (Integer)id :(id!=null?new Integer(id.toString()):0));
		td.setPid(pid instanceof Integer? (Integer)pid :(pid!=null?new Integer(pid.toString()):null));
		td.setCode(code instanceof String? (String)code :(code!=null?code.toString():""));
		td.setText(text instanceof String? (String)text :(text!=null?text.toString():""));

		return td;
	}

	
	
	private  Map<String,Method> getMapReadMethod(Class clazz){
		
		Map<String,Method> map=new HashMap<String,Method>();
		PropertyDescriptor	ps[]=PropertyUtils.getPropertyDescriptors(clazz);
		for (PropertyDescriptor propertyDescriptor : ps) {
			
			map.put(propertyDescriptor.getName(), propertyDescriptor.getReadMethod());
		}
		return map;
	}
	
	
	private Method[] getReadMethod(Class clazz, String idp, String pidp,
			String textp,String codep){
		Map<String,Method> map=getMapReadMethod(clazz);
		Method[] methods=new Method[4];
		methods[0]=map.get(idp);
		methods[1]=map.get(pidp);
		methods[2]=map.get(textp);
		methods[3]=map.get(codep);
		return methods;		
		
		
	}
	
	public List getBornTree(List list, String idp, String pidp,
			String textp, TreeURL url,String codep) {
		Object object = null;
		List rlist = new ArrayList(list.size());
		TreeDatas tree=null;
		Method[]methods=null;
		try {
			for (Iterator iterator = list.iterator(); iterator.hasNext();) {
				object = iterator.next();

				if (object instanceof Map) {
					tree=getMapTreeData((Map) object, idp, pidp,
							textp,codep);
				} else if(object!=null){
					
					if(methods==null)
					methods=getReadMethod(object.getClass(), idp,  pidp, textp, codep);
					tree=getTreeData(methods,object);

				}
				if (url != null) {
					tree.setUrl(url.getURL(object));
				}
				rlist.add(tree);
			}
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		}
			
			return rlist;
		

		

	}

}
